package com.songqintuan.platform.page;

import javax.faces.model.SelectItem;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.songqintuan.common.bean.Page;
import com.songqintuan.common.page.IPojoPage;
import com.songqintuan.common.page.PojoPage;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.SystemUser;
import com.songqintuan.persist.service.hibernate.ISystemUserService;
import com.songqintuan.platform.common.constant.PlatformConstants;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;

public class SystemUserPage extends PojoPage<SystemUser, Long> implements
		IPojoPage<SystemUser, Long>, java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224868L;

	public void getPageDatas() {
		Page<SystemUser, Long> curPage = new Page<SystemUser, Long>();
		Integer pPageNo = this.getPageNo();
		if (null == pPageNo) {
			pPageNo = new Integer(1);
		}
		Integer pPageSize = this.getPageSize();
		if (null == pPageSize) {
			pPageSize = new Integer(PlatformConstants.DEFAULT_ONE_PAGE_NUMBER);
		}
		curPage = this.getData(pPageNo, pPageSize, this.getParam());
		SelectItem[] tempPageItem = new SelectItem[(int) curPage
				.getTotalPageCount()];
		for (int j = 0; j < tempPageItem.length; j++) {
			tempPageItem[j] = new SelectItem(new Integer(j + 1),
					String.valueOf(j + 1));
		}
		this.setPageItems(tempPageItem);
		this.setPage(curPage);
	}

	public SystemUserPage(Integer pageNo, Integer pageSize, SystemUser param) {
		super();
		this.setPageSize(pageSize);
		this.setPageNo(pageNo);
		this.setParam(param);

	}

	// 所有的查询均根据param的值确定
	public Page<SystemUser, Long> getData(int pageNo, int pageSize, SystemUser param) {// param
																					// is
																					// statuscode
																					// and
																					// customer
		Page<SystemUser, Long> resultPage = new Page<SystemUser, Long>();
		ISystemUserService<SystemUser, Long> objService = (ISystemUserService<SystemUser, Long>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.SYSTEM_USER_SERVICE_BEAN_NAME);
		objService.setEntityClass(SystemUser.class);
		objService.setPKClass(Long.class);
		Criteria criteria = objService.createCriteria(null);
		if (null != param.getUserName() && !("".equals(param.getUserName()))) {
			criteria.add(Restrictions.like("userName", "%" + param.getUserName() + "%"));
		}
		if (null != param.getSex() && !("".equals(param.getSex()))) {
			criteria.add(Restrictions.eq("sex", param.getSex()));
		}
		if (null != param.getLoginName() && !("".equals(param.getLoginName()))) {
			criteria.add(Restrictions.eq("loginName", param.getLoginName()));
		}
		if (null != param.getMobile() && !("".equals(param.getMobile()))) {
			criteria.add(Restrictions.like("mobile", "%" + param.getMobile() + "%"));
		}
		if (null != param.getPhone() && !("".equals(param.getPhone()))) {
			criteria.add(Restrictions.like("phone", "%" + param.getPhone() + "%"));
		}
		if (null != param.getEmail() && !("".equals(param.getEmail()))) {
			criteria.add(Restrictions.like("email", "%" + param.getEmail()+ "%"));
		}
		if (null != param.getQicq() && !("".equals(param.getQicq()))) {
			criteria.add(Restrictions.like("qicq", "%" + param.getQicq() + "%"));
		}
		if (null != param.getMsn() && !("".equals(param.getMsn()))) {
			criteria.add(Restrictions.like("msn", "%" + param.getMsn() + "%"));
		}
		if (null != param.getStatus() && !("".equals(param.getStatus()))) {
			criteria.add(Restrictions.eq("status", param.getStatus()));
		}
		resultPage = objService.pagedQuery(criteria, pageNo, pageSize); 
		return resultPage;
	}
}
